package com.google.code.p.odbcconnectionchecker.UI;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JTextField;

import com.google.code.p.odbcconnectionchecker.ConnectionCheckerPopupMenu;
import com.google.code.p.odbcconnectionchecker.ConnectionMenu;

import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Font;
import java.io.File;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;

public class ConnectionWindow extends JFrame {
	
	private static final long serialVersionUID = 1L;
	
	private JPanel contentPane;
	private JTextField txtJdbcConnectionString;
	private JTextField txtUsername;
	private JTextField txtPassword;
	private JTextField txtDriver;
	private JTextField txtName;
	JSpinner spnRefreshTime;
	
	private ConnectionMenu cm = null;
	
	private static ConnectionCheckerPopupMenu popupMenu;

	private JTextField txtDriverURL;

	/**
	 * Create the frame.
	 */
	public ConnectionWindow() {
		setTitle("Create New Connection");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BorderLayout(0, 0));
		
		JLabel lblNewLabel = new JLabel("Create New Connection");
		lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		contentPane.add(lblNewLabel, BorderLayout.NORTH);
		
		JPanel bottomPanel = new JPanel();
		contentPane.add(bottomPanel, BorderLayout.SOUTH);
		bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.X_AXIS));
		
		JButton btnSaveConnection = new JButton("Save");
		btnSaveConnection.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (cm != null) {
					cm.getJdbcSetting().setConnectionDriver(txtDriver.getText());
					cm.getJdbcSetting().setConnectionURL(txtJdbcConnectionString.getText());
					cm.getJdbcSetting().setUsername(txtUsername.getText());
					cm.getJdbcSetting().setPassword(txtPassword.getText());
					cm.setConnectionName(txtName.getText());
					cm.getJdbcSetting().setDriverURL(txtDriverURL.getText());
					cm.setRefreshTimeSec((Integer) spnRefreshTime.getValue());
					
				} else { 	
					ConnectionMenu new_cm = new ConnectionMenu();
					new_cm.getJdbcSetting().setConnectionDriver(txtDriver.getText());
					new_cm.getJdbcSetting().setConnectionURL(txtJdbcConnectionString.getText());
					new_cm.getJdbcSetting().setUsername(txtUsername.getText());
					new_cm.getJdbcSetting().setPassword(txtPassword.getText());
					new_cm.setConnectionName(txtName.getText());
					new_cm.getJdbcSetting().setDriverURL(txtDriverURL.getText());
					new_cm.setRefreshTimeSec((Integer)spnRefreshTime.getValue());
					popupMenu.newConnection(new_cm);
				}
				ConnectionWindow.this.dispose();
			}
		});
		bottomPanel.add(btnSaveConnection);
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ConnectionWindow.this.dispose();
			}
		});
		bottomPanel.add(btnCancel);
		
		JPanel centerPanel = new JPanel();
		contentPane.add(centerPanel, BorderLayout.CENTER);
		centerPanel.setLayout(new GridLayout(0, 2, 0, 0));
		
		JLabel lblName = new JLabel("Connection Name");
		centerPanel.add(lblName);
		
		txtName = new JTextField();
		centerPanel.add(txtName);
		txtName.setColumns(10);
		
		JLabel lblDriverURL = new JLabel("Driver URL");
		centerPanel.add(lblDriverURL);
		
		JPanel driverUrlPanel = new JPanel();
		centerPanel.add(driverUrlPanel);
		driverUrlPanel.setLayout(new GridLayout(1, 0, 0, 0));
		
		txtDriverURL = new JTextField();
		driverUrlPanel.add(txtDriverURL);
		txtDriverURL.setColumns(10);
		
		JButton btnDriverURL = new JButton("Pick Dir");
		btnDriverURL.addActionListener(
				new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent arg0) {
						//Check whether only one file is needed for properly loading JDBC drivers (i.e. Teradata)
						//Use File Filter for showing only jar files
						JFileChooser chooser = new JFileChooser();
						chooser.setMultiSelectionEnabled(false);
						int option = chooser.showOpenDialog(ConnectionWindow.this);
						if (option == JFileChooser.APPROVE_OPTION){ 
							File sf = chooser.getSelectedFile();
							if (sf.getName().endsWith("jar")) {  
								txtDriverURL.setText(sf.getAbsoluteFile().toString());
							}
						}
					}
				}
		);
		driverUrlPanel.add(btnDriverURL);
		
		JLabel lblDriver = new JLabel("Driver");
		centerPanel.add(lblDriver);
		
		txtDriver = new JTextField();
		centerPanel.add(txtDriver);
		txtDriver.setColumns(10);
		
		JLabel lblJdbcConnectionString = new JLabel("JDBC ConnectionString");
		centerPanel.add(lblJdbcConnectionString);
		
		txtJdbcConnectionString = new JTextField();
		centerPanel.add(txtJdbcConnectionString);
		txtJdbcConnectionString.setColumns(10);
		
		JLabel lblUsername = new JLabel("Username");
		centerPanel.add(lblUsername);
		
		txtUsername = new JTextField();
		centerPanel.add(txtUsername);
		txtUsername.setColumns(10);
		
		JLabel lblPassword = new JLabel("Password");
		centerPanel.add(lblPassword);
		
		txtPassword = new JTextField();
		centerPanel.add(txtPassword);
		txtPassword.setColumns(10);
		
		JLabel lblRefresh = new JLabel("Refresh Frequency (s)");
		centerPanel.add(lblRefresh);
		
		spnRefreshTime = new JSpinner();
		spnRefreshTime.setModel(new SpinnerNumberModel(1, 1, 3600, 1));
		spnRefreshTime.setValue(60);
		centerPanel.add(spnRefreshTime);
		
		setVisible(true);		
	}
	
	public void existingConnection(ConnectionMenu cm) {
		this.cm = cm;
		txtJdbcConnectionString.setText(cm.getJdbcSetting().getConnectionURL());
		txtUsername.setText(cm.getJdbcSetting().getUsername());
		txtPassword.setText(cm.getJdbcSetting().getPassword());
		txtDriver.setText(cm.getJdbcSetting().getConnectionDriver());
		txtName.setText(cm.getConnectionName());
		txtDriverURL.setText(cm.getJdbcSetting().getDriverURL());
		spnRefreshTime.setValue(cm.getRefreshTimeSec());
	}
	
	public static ConnectionCheckerPopupMenu getPopupMenu() {
		return popupMenu;
	}

	public static void setPopupMenu(ConnectionCheckerPopupMenu popupMenu) {
		ConnectionWindow.popupMenu = popupMenu;
	}
}
